

import requests
from bs4 import BeautifulSoup
import time


header = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 Edg/109.0.1518.55"
}

def getaddrs(biaoqian):
    soup = BeautifulSoup(biaoqian, "html.parser")
    tags = soup.find_all("img", class_="mimg")
    print(len(tags))
    for tag in tags:
        try:
            image = tag["src"]
        except KeyError:
            image = tag["data-src"]
        addrs.append(image)


def saveimg(addr):
    print(time.time())
    fileName = "D:/PythonProjects/createmydataset/images/__ignore__/" + str(int(time.time() * 1000)) + ".jpg"
    r = requests.get(addr, headers=header)
    f = open(fileName, "wb")
    f.write(r.content)
    f.close()



addrs = []

for i in range(0, 5):
    url = 'https://cn.bing.com/images/async?q=%e6%96%bd%e5%b7%a5%e7%8e%b0%e5%9c%ba&first=' + str(i * 35 + 1) + '&count=35&mmasync=1'
    r = requests.get(url, headers=header)
    r.encoding = "utf-8"
    demo = r.text
    getaddrs(demo)

print(len(addrs))

for addr in addrs:
    saveimg(addr)
